package com.cxsw.rms.interceptor;

import com.cxsw.rms.utils.Constants;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录拦截器
 *
 * @author Han Fuhao
 * @date 2023/05/15
 */
public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 拦截器访问前处理
     *
     * @param request  请求
     * @param response 响应
     * @param handler  处理程序
     * @return boolean
     * @throws Exception 异常
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object user = session.getAttribute(Constants.SESSION_USER);
        if (user == null){
            response.sendRedirect("/toLogin");
            return false;
        }else {
            return true;
        }
    }
}
